package com.wpre.lotterysystem.service.activitystatus.operator;

import com.wpre.lotterysystem.dao.dataobject.ActivityDO;
import com.wpre.lotterysystem.dao.mapper.ActivityMapper;
import com.wpre.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.wpre.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.wpre.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import com.wpre.lotterysystem.service.enums.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-12-24
 * Time: 20:03
 */
@Component
public class ActivityOperator extends AbstractActivityOperator {

    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        if (convertActivityStatusDTO.getActivityId() == null
                || convertActivityStatusDTO.getTargetActivityStatus() == null) {
            return false;
        }
        //从数据库中查询
        ActivityDO activityDO = activityMapper.selectActivityById(convertActivityStatusDTO.getActivityId());
        if (activityDO == null) {
            return false;
        }

        //当前活动状态
        if (convertActivityStatusDTO.getTargetActivityStatus().name()
                .equals(activityDO.getStatus())) {
            return false;
        }
        //奖品是否全部抽完
        int count = activityPrizeMapper.countPrize(convertActivityStatusDTO.getActivityId(),
                ActivityPrizeStatusEnum.INIT.name());
        if (count > 0) {
            //奖品还没抽完
            return false;
        }
        return true;
    }

    //状态扭转
    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            activityMapper.updateStatus(convertActivityStatusDTO.getActivityId(),
                    convertActivityStatusDTO.getTargetActivityStatus().name());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
